<?php

namespace Mobile\Controller;

/**
 * Description of ParentController.class控制器实例
 * 
 * @author songanwei
 * @date 2016-1-22
 */
class ParentController extends \Common\Controller\BaseController {

    protected function _initialize() {
        parent::_initialize();
        $hotelId = I('post.hotel');
        if(!empty($hotelId)){
            $this->checkHotelStatus($hotelId);
            $this->switchTimeUtc($hotelId);
        }
    }

   /**
     * 言切时区
     * @modify wenhui 2017-06-22    创建
     */
    protected function switchTimeUtc($hotelId) {
        // 客栈时区
        $hotelEntityInfo= D('HotelEntity')->getInfo($hotelId);
        $universalTimeModel = D('UniversalTime');
        $universalTimeInfo = $universalTimeModel->field('etc')->where(['id' => $hotelEntityInfo['universaltime_id']])->find();
        $universalTimeEtc = $universalTimeInfo ? $universalTimeInfo['etc'] : 'Etc/GMT-8';
        date_default_timezone_set($universalTimeEtc);
    }

    /**
     * 移动端检查token数据
     * @modify songanwei 2016-01-22
     * @param integer $userId 用户ID
     * @param string $token 用户Token
     */
    protected function checkMobileToken($userId, $token) {
        $adminLoginLogic = A('Common/AdminLogin', 'Logic');
        $result = $adminLoginLogic->checkToken($userId, $token);
        if (!$result) {
            returnJson(403, 207, 'error');
        }
    }
    
    /**
     * 移动端检查客栈开通状况
     * @modify paddy 2017-03-15
     * @param integer $hotelId 客栈ID
     */
    protected function checkHotelStatus($hotelId) {
        $hotelLogic = A('Common/Hotel', 'Logic');
        if(!$hotelLogic->checkHotelAppEnable($hotelId)){
            returnJson(403, 206, 'error');
        }
    }

    /**
     * @description 是否是义卖客栈 
     * @modify wenhui 2017-09-12
     * @param int $hotelId 客栈id
     * @param int $maxRooms 最大房间数
     * @param int $count 房间数量
     */
    protected function checkUsageHotel($hotelId) {
        $hotelInfo = D('HotelEntity')->getInfo($hotelId);
        $roomInfo = D('RoomInfo')->getInfoByHotel($hotelId);
        $isMax = false;
        $isFreeHotel = false;
        if ($hotelInfo['usage'] == 4) {
            $isFreeHotel = true;
        }
        $existsRooms = isset($roomInfo) ? count($roomInfo) : 0;

        return array('isFreeHotel' => $isFreeHotel , 'existsRooms' => $existsRooms);
    }
}
